package com.project.controller.activity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.controller.BaseController;
import com.project.entity.activity.Activity;
import com.project.entity.enumcommon.PublishStatusEnum;
import com.project.service.activity.ActivityService;

@Controller
@RequestMapping("/activity")
public class ActivityController extends BaseController<Activity>{

	@Autowired
	private ActivityService activityService;

	
	@RequestMapping("list")
	public String list(Model model,@ModelAttribute Activity activity) {
		model.addAttribute("entity", activity);
		model.addAttribute("maps", PublishStatusEnum.values());
		model.addAttribute("list", activityService.getList(activity));
		return "/activity/list";
	}
	
	@RequestMapping("add")
	public String add(Model model) {
		model.addAttribute("entity", getEntity());
		return "/activity/update";
	}
	
	@RequestMapping("update")
	public String update(Model model,@ModelAttribute Activity entity) {
		model.addAttribute("entity", getEntity());
		return "/activity/update";
	}
	
	@ResponseBody
	@RequestMapping("save")
	public Long save(Model model,@ModelAttribute(value="entity") Activity entity,String delIds) {
		 activityService.saveEntity(entity,delIds);
		 return entity.getId();
	}
	
	@RequestMapping("publish")
	@ResponseBody
    public String publish(Model model) {
		Activity activity = getEntity();
		if(activity.getDr()==1){
			return "该活动已删除";
		}
		activity.setPublishStatus(PublishStatusEnum.P);
		activityService.update(activity);
        return "true";
    }
	
	@RequestMapping("view")
	public String view(Model model) {
		model.addAttribute("entity", activityService.getById(getEntity()));
		model.addAttribute("maps", PublishStatusEnum.values());
		return "/activity/view";
	}
	
	/**
	 * 逻辑删除
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String delete(){
		Activity activity = getEntity();
		if(PublishStatusEnum.P.equals(activity.getPublishStatus())){
			return "该活动已发布";
		}
		activity.setDr(1);
		activityService.update(activity);
		return "true";
	}
	
	@Override
	protected Class<Activity> getClazz() {
		return Activity.class;
	}
}
